package com.hk.webapp.controller.admin;

import com.hk.webapp.bean.SysRole;
import com.hk.webapp.bean.SysRoleAction;
import com.hk.webapp.service.SysRoleActionService;
import com.hk.webapp.service.SysRoleService;
import com.hk.webapp.utils.EHCacheUtils;
import com.hk.webapp.vo.EditRoleVo;
import com.hk.webapp.vo.PageVo;
import com.hk.webapp.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import net.sf.ehcache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;

@RestController
public class SysRoleController extends AdminBaseController {

    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysRoleActionService sysRoleActionService;
    @Resource
    private CacheManager cacheManager;

    @PostMapping("/SysRole/GetAllRole")
    public ResultVo getAllRole(@RequestParam(defaultValue = "0") Integer full){
        List<SysRole> list= sysRoleService.getEntityList();
        if (full==0){
            return ResultVo.createBySuccessData(list.parallelStream().map(c->{
                HashMap<String,Object> map= Maps.newHashMap();
                map.put("id",c.getId());
                map.put("name",c.getName());
                return map;
            }).collect(Collectors.toList()));
        }else{
            return ResultVo.createBySuccessData(list);
        }
    }

    @PostMapping("/SysRole/GetPageList")
    public ResultVo getPageList(@RequestBody PageVo vo){
        PageInfo<SysRole> list= sysRoleService.getPageList(vo);
        return ResultVo.createBySuccessData(list.getList(),list.getTotal());
    }
    @GetMapping("/SysRole/GetById")
    public ResultVo getById(Integer id){
        if (id<=0){
            return ResultVo.createByError("非法参数");
        }
        SysRole sysRole= sysRoleService.getEntityById(id);
        if (sysRole==null){
            return ResultVo.createByError("数据不存在");
        }
        return ResultVo.createBySuccessData(sysRole);
    }

    @PostMapping("/SysRole/EditRole")
    public ResultVo editRole(@RequestBody @Valid EditRoleVo vo){

        SysRole sysRole=sysRoleService.getEntityById(vo.getId());
        if (sysRole==null){
            return ResultVo.createByError("角色更新失败");
        }
        //更新角色信息
        sysRole.setName(vo.getName());
        sysRole.setDescribe(vo.getDescribe());

        if (sysRoleService.update(sysRole)<=0) {
            return  ResultVo.createByError("更新失败");
        }
        //删除角色权限关系表
        sysRoleActionService.deleteByRoleIds(asList(sysRole.getId()));
        Integer count=0;
        //创建角色权限关系表
        for (Integer item:vo.getActionIds()) {
            SysRoleAction sysRoleAction=new SysRoleAction();
            sysRoleAction.setSysActionId(item);
            sysRoleAction.setSysRoleId(sysRole.getId());
            sysRoleAction.setCreateTime(new Date());
            sysRoleAction.setIsDeleted(0);

            if(sysRoleActionService.insert(sysRoleAction)>0){
                count++;
            }
        }
        if (count == vo.getActionIds().size())
        {
            EHCacheUtils.deleteAllCache(cacheManager);
            return  ResultVo.createBySuccessData("succeed");
        }
        else
        {
            return ResultVo.createByError("权限插入有误");
        }
    }

    @PostMapping("/SysRole/AddRole")
    public ResultVo addRole(@RequestHeader(value = "userId", required = false) Integer userId,@RequestBody @Valid EditRoleVo vo){
        SysRole sysRole=new SysRole();
        sysRole.setName(vo.getName());
        sysRole.setDescribe(vo.getDescribe());
        sysRole.setState(1);
        sysRole.setCreateTime(new Date());
        sysRole.setCreator(userId);
        sysRoleService.insert(sysRole);
        if (sysRole.getId()==0){
            return ResultVo.createByError("角色添加失败");
        }
        //创建角色权限关系表
        Integer count=0;
        //创建角色权限关系表
        for (Integer item:vo.getActionIds()) {
            SysRoleAction sysRoleAction=new SysRoleAction();
            sysRoleAction.setSysActionId(item);
            sysRoleAction.setSysRoleId(sysRole.getId());
            sysRoleAction.setCreateTime(new Date());
            sysRoleAction.setIsDeleted(0);

            if(sysRoleActionService.insert(sysRoleAction)>0){
                count++;
            }
        }
        if (count == vo.getActionIds().size())
        {
            return  ResultVo.createBySuccessData("succeed");
        }
        else
        {
            return ResultVo.createByError("权限插入有误");
        }
    }
}
